<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class feed extends CI_Controller {
    public function addFeed()
    {
    	$session = $this->session->all_userdata();
    	if(!isset($session['username'])){
    		$data['controller']='';
		   	$data['name_view']='home';
    		$this->load->view('Aquasprite',$data);
    		return;
    	}
    	$data = array();
    	$this->load->model('user_model');
    	if ($_SERVER['REQUEST_METHOD'] == 'POST'){  		
    		$user= $this->user_model->getUserByUserName($session['username']);
    		$user_id=$user['id'];
    		$name=$_POST['fname'];
            $source = $_POST['source'];
            $link=$_POST['link'];
            $status = $_POST['status']; 

            $feedData = array(
				'user_id'=> $user_id,
				'name'=> $name,
				'source'=> $source,
				'link' => $link,
            	'status'=>$status,
			);
			$insert=$this->db->insert('feed', $feedData);
            if($insert){
           		$data['controller']='';
		   		$data['name_view']='home';
		    	$this->load->view('Aquasprite',$data);
            }
 			
    	}
    	$data['controller']='feed/';
    	$data['name_view']='addFeed';
    	$this->load->view('Aquasprite',$data);
    }
    public function updateAUTFeed(){
    		$this->load->model('user_model');
    		//-------------AUT's news--------------------
		
	        $doc = new DOMDocument();
			//$doc->load( './feed/xml/id1.xml' );
			$doc->load('http://www.aut.ac.nz/rss-feeds/news');
			
			$feeds = $doc->getElementsByTagName("item");
			$arrayXmlAUTnews=array();
			$countfeedXml = 0;
			
			foreach( $feeds as $feed )
			{
				if($countfeedXml<3){
					$countfeedXml++;
					$titles = $feed->getElementsByTagName("title");
					$title = $titles->item(0)->nodeValue;
					
					$describtions = $feed->getElementsByTagName("link");
					$description = $describtions->item(0)->nodeValue;
					$arrayXmlAUTnews[] = array($title,$description);
				}
			}
    		$count = 1;
			foreach ($arrayXmlAUTnews as $key){
				$arrayAUT_library = array(
					'content' => $key[0],
					'source' => $key[1],
				);
				$this->user_model->updateAutLibrary($count,$arrayAUT_library);
				$count ++;
			}
			
			//-------------AUT's Library news--------------------
			
	        $doc = new DOMDocument();
			//$doc->load( './feed/xml/id1.xml' );
			$doc->load('http://www.aut.ac.nz/rss-feeds/library-news');
			
			$feeds = $doc->getElementsByTagName("item");
			$arrayXmlAUTLinews=array();
			$countfeedXml = 0;
			
			foreach( $feeds as $feed )
			{
				if($countfeedXml<3){
					$countfeedXml++;
					$titles = $feed->getElementsByTagName("title");
					$title = $titles->item(0)->nodeValue;
					
					$describtions = $feed->getElementsByTagName("link");
					$description = $describtions->item(0)->nodeValue;				
					$arrayXmlAUTLinews[] = array($title,$description);
				}
			}
			$count = 1;
			foreach ($arrayXmlAUTLinews as $key){
				$arrayAUT = array(
					'content' => $key[0],
					'source' => $key[1],
				);
				$this->user_model->updateAutNew($count,$arrayAUT);
				$count ++;
			}
			
			$this->load->model('user_model');
    //-------------Weather at Sungapore--------------------
    	$doc = new DOMDocument();
		$doc->load('http://www.miniweather.com/rss/?poi=singapore');
		
		$feeds = $doc->getElementsByTagName("item");
		$arrayXmlSinWeather=array();
		$countfeedXml = 0;
		
		foreach( $feeds as $feed )
		{
			if($countfeedXml<3){
				$countfeedXml++;
				$titles = $feed->getElementsByTagName("title");
				$title = $titles->item(0)->nodeValue;
				
				$describtions = $feed->getElementsByTagName("description");
				$description = $describtions->item(0)->nodeValue;

				$arrayXmlSinWeather[] = array($title,$description);
			}
		}
		if(isset($arrayXmlSinWeather[0][1])){
			$arraySin = array(
				'content' => $arrayXmlSinWeather[0][1],
				'country' => 'Singarpo',
			);
			if($arrayXmlSinWeather[0][1] != ""){
				$this->user_model->updateWeather(1,$arraySin);
			}
		}
		//-------------Weather at Wellington city--------------------		
       	$doc = new DOMDocument();
		$doc->load('http://www.miniweather.com/rss/?poi=Wellington,+Wellington');
		
		$feeds = $doc->getElementsByTagName("item");
		$arrayXmlNzlWeather=array();
		$countfeedXml = 0;
		
		foreach( $feeds as $feed )
		{
			if($countfeedXml<3){
				$countfeedXml++;
				$titles = $feed->getElementsByTagName("title");
				$title = $titles->item(0)->nodeValue;
				
				$describtions = $feed->getElementsByTagName("description");
				$description = $describtions->item(0)->nodeValue;
				$arrayXmlNzlWeather[] = array($title,$description);
			}
		}
		if(isset($arrayXmlNzlWeather[0][1])){
			$arrayNzl = array(
				'content' => $arrayXmlNzlWeather[0][1],
				'country' => 'New Zeland',
			);
			if($arrayXmlNzlWeather[0][1] != ""){
				$this->user_model->updateWeather('2',$arrayNzl);
			}
		}
		//-------------Weather Ho Chi Minh City--------------------
		
        $doc = new DOMDocument();
		$doc->load('http://www.miniweather.com/rss/?poi=Ho+Chi+Minh+City,+Ho+Chi+Minh+City');
		
		$feeds = $doc->getElementsByTagName("item");
		$arrayXmlHCMWeather=array();
		$countfeedXml = 0;
		
		foreach( $feeds as $feed )
		{
			if($countfeedXml<3){
				$countfeedXml++;
				$titles = $feed->getElementsByTagName("title");
				$title = $titles->item(0)->nodeValue;
				
				$describtions = $feed->getElementsByTagName("description");
				$description = $describtions->item(0)->nodeValue;
				$arrayXmlHCMWeather[] = array($title,$description);
			}
		}
		if(isset($arrayXmlHCMWeather[0][1])){
			$arrayHCM = array(
				'content' => $arrayXmlHCMWeather[0][1],
				'country' => 'New Zeland',
			);
		
			if($arrayXmlHCMWeather[0][1] != ""){
				$this->user_model->updateWeather('3',$arrayHCM);
			}
		}
		redirect('/');
    }
  
}
?>